package andriod.service;

import andriod.dao.PersistDao;

import com.jcrj.entity.OInfo;
import com.jcrj.entity.OInfoContent;
import com.jcrj.entity.ONotice;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.SQLException;
import java.util.List;

import org.hibernate.lob.SerializableBlob;

public class ArdInfoService {
	private PersistDao persistDao;

	public PersistDao getPersistDao() {
		return this.persistDao;
	}

	public void setPersistDao(PersistDao persistDao) {
		this.persistDao = persistDao;
	}

	public List<OInfo> getInfoList() {
		String hql = "from OInfo o where o.del='0' and o.columnId = 1 order by o.id desc";
		return this.persistDao.find(hql);
	}

	public long countInfo() {
		String hql = "select count(o.id) from OInfo o where o.del='0' and o.columnId = 1";
		return this.persistDao.findForLong(hql);
	}

	public List<ONotice> getNoticeList(String orgIds, boolean role) {
		String hql = "from ONotice";
		if (!role)
			hql = hql + " where orgId in(" + orgIds + ")";
		else {
			hql = hql + " where 1=1";
		}
		hql = hql + " order by createTime desc";
		return this.persistDao.find(hql);
	}

	public long countNotice(String orgIds, boolean role) {
		String hql = "select count(id) from ONotice";
		if (!role)
			hql = hql + " where orgId in(" + orgIds + ")";
		else {
			hql = hql + " where 1=1";
		}
		return this.persistDao.findForLong(hql);
	}

	public String getBLOBString(String contentId) throws IOException,
			SQLException {
		StringBuffer content = new StringBuffer();
		String sql = "select CONTENT from O_INFO_CONTENT where ID='"
				+ contentId + "'";
		List bs = this.persistDao.query(sql);
		if ((bs != null) && (!bs.isEmpty())) {
			InputStream instream = ((SerializableBlob) bs.get(0))
					.getBinaryStream();
			byte[] mFileBody = new byte[1024];
			int len = 0;
			while ((len = instream.read(mFileBody)) != -1) {
				instream.read(mFileBody, 0, 1024);
				content.append(new String(mFileBody, 0, len));
			}
			instream.close();
		}
		return content.toString();
	}
	
	public Blob getBlobContent(String contentId) throws IOException,
			SQLException {
		String sql = "select CONTENT from O_INFO_CONTENT where ID='"
				+ contentId + "'";
		@SuppressWarnings("rawtypes")
		List bs = this.persistDao.query(sql);
		
		
		if((bs != null) && (!bs.isEmpty())){
			return  (Blob)bs.get(0);
		}
		return null;
	}
	
	
	public SerializableBlob getContent(String contentId) throws IOException,
			SQLException {
		String sql = "select CONTENT from O_INFO_CONTENT where ID='"
				+ contentId + "'";
		@SuppressWarnings("rawtypes")
		List bs = this.persistDao.query(sql);
		if((bs != null) && (!bs.isEmpty())){
			return (SerializableBlob) bs.get(0);
		}
		return null;
	}
	
	public void saveInfoContent(String contentId,byte[] content){
		persistDao.updateInfoContent(contentId, content);
	}
	
	public void saveInfoContent(String contentId,InputStream inputStream,int fileSize){
//		persistDao.updateInfoContent(contentId, inputStream,fileSize);
	}
	
}